<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
<!--    <el-form-item label="商品名称" prop="commodityName">-->
<!--      <el-input v-model="dataForm.commodityName" placeholder="商品名称"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="商品编号" prop="commodityNo">
      <el-input v-model="dataForm.commodityNo" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="类别" prop="commodityType">
      <el-input v-model="dataForm.commodityType" placeholder="类别"></el-input>
    </el-form-item>
    <el-form-item label="光瓶编号" prop="bottleProductId">
      <el-input v-model="dataForm.bottleProductId" placeholder="光瓶编号"></el-input>
    </el-form-item>
      <el-form-item label="克重" prop="productWeight">
        <el-input v-model="dataForm.productWeight" placeholder="克重"></el-input>
      </el-form-item>
      <el-form-item label="容量" prop="productVolume">
        <el-input v-model="dataForm.productVolume" placeholder="容量"></el-input>
      </el-form-item>
      <el-form-item label="瓶口外径" prop="bottleOutDiameter">
        <el-input v-model="dataForm.bottleOutDiameter" placeholder="瓶口外径"></el-input>
      </el-form-item>
      <el-form-item label="瓶高" prop="bottleHeight">
        <el-input v-model="dataForm.bottleHeight" placeholder="瓶高"></el-input>
      </el-form-item>
    <el-form-item label="模具" prop="modelId">
      <el-input v-model="dataForm.modelId" placeholder="模具"></el-input>
    </el-form-item>
    <el-form-item label="盖子" prop="bottleCapId">
      <el-input v-model="dataForm.bottleCapId" placeholder="盖子"></el-input>
    </el-form-item>
    <el-form-item label="喷头" prop="bottleSprayHeadId">
      <el-input v-model="dataForm.bottleSprayHeadId" placeholder="喷头"></el-input>
    </el-form-item>
    <el-form-item label="肩套" prop="bottlePartId">
      <el-input v-model="dataForm.bottlePartId" placeholder="肩套"></el-input>
    </el-form-item>
    <el-form-item label="客户" prop="clientId">
      <el-input v-model="dataForm.clientId" placeholder="客户"></el-input>
    </el-form-item>
    <el-form-item label="中套" prop="centerCoverId">
      <el-input v-model="dataForm.centerCoverId" placeholder="中套"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="imagePath">
      <el-input v-model="dataForm.imagePath" placeholder="商品图片"></el-input>
    </el-form-item>
      <el-form-item label="深加工" prop="deepCategoryName">
        <el-input v-model="dataForm.deepCategoryName" placeholder="深加工"></el-input>
      </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
<!--    <el-form-item label="商品价格" prop="commodityPrice">-->
<!--      <el-input v-model="dataForm.commodityPrice" placeholder="商品价格"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>

    <el-form-item label="版本号" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本号"></el-input>
    </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          commodityName: '',
          commodityNo: '',
          commodityType: '',
          bottleProductId: '',
          modelId: '',
          bottleCapId: '',
          bottleSprayHeadId: '',
          bottlePartId: '',
          clientId: '',
          centerCoverId: '',
          imagePath: '',
          status: '',
          commodityPrice: '',
          createTime: '',
          createUser: '',
          updateUser: '',
          updateTime: '',
          remark: '',
          productWeight: '',
          productVolume: '',
          bottleOutDiameter: '',
          version: '',
          bottleHeight: '',
          deepCategoryId: '',
          deepCategoryName: ''
        },
        dataRule: {
          commodityName: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          commodityNo: [
            { required: true, message: '商品编号不能为空', trigger: 'blur' }
          ],
          commodityType: [
            { required: true, message: '类别不能为空', trigger: 'blur' }
          ],
          bottleProductId: [
            { required: true, message: '光瓶编号不能为空', trigger: 'blur' }
          ],
          modelId: [
            { required: true, message: '模具不能为空', trigger: 'blur' }
          ],
          bottleCapId: [
            { required: true, message: '盖子不能为空', trigger: 'blur' }
          ],
          bottleSprayHeadId: [
            { required: true, message: '喷头不能为空', trigger: 'blur' }
          ],
          bottlePartId: [
            { required: true, message: '肩套不能为空', trigger: 'blur' }
          ],
          clientId: [
            { required: true, message: '客户ID不能为空', trigger: 'blur' }
          ],
          centerCoverId: [
            { required: true, message: '中套不能为空', trigger: 'blur' }
          ],
          imagePath: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          commodityPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          productWeight: [
            { required: true, message: '克重不能为空', trigger: 'blur' }
          ],
          productVolume: [
            { required: true, message: '容量不能为空', trigger: 'blur' }
          ],
          bottleOutDiameter: [
            { required: true, message: '瓶口外径不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ],
          bottleHeight: [
            { required: true, message: '瓶高不能为空', trigger: 'blur' }
          ],
          deepCategoryId: [
            { required: true, message: '深加工ID不能为空', trigger: 'blur' }
          ],
          deepCategoryName: [
            { required: true, message: '深加工不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/commodityinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commodityName = data.commodityInfo.commodityName
                this.dataForm.commodityNo = data.commodityInfo.commodityNo
                this.dataForm.commodityType = data.commodityInfo.commodityType
                this.dataForm.bottleProductId = data.commodityInfo.bottleProductId
                this.dataForm.modelId = data.commodityInfo.modelId
                this.dataForm.bottleCapId = data.commodityInfo.bottleCapId
                this.dataForm.bottleSprayHeadId = data.commodityInfo.bottleSprayHeadId
                this.dataForm.bottlePartId = data.commodityInfo.bottlePartId
                this.dataForm.clientId = data.commodityInfo.clientId
                this.dataForm.centerCoverId = data.commodityInfo.centerCoverId
                this.dataForm.imagePath = data.commodityInfo.imagePath
                this.dataForm.status = data.commodityInfo.status
                this.dataForm.commodityPrice = data.commodityInfo.commodityPrice
                this.dataForm.createTime = data.commodityInfo.createTime
                this.dataForm.createUser = data.commodityInfo.createUser
                this.dataForm.updateUser = data.commodityInfo.updateUser
                this.dataForm.updateTime = data.commodityInfo.updateTime
                this.dataForm.remark = data.commodityInfo.remark
                this.dataForm.productWeight = data.commodityInfo.productWeight
                this.dataForm.productVolume = data.commodityInfo.productVolume
                this.dataForm.bottleOutDiameter = data.commodityInfo.bottleOutDiameter
                this.dataForm.version = data.commodityInfo.version
                this.dataForm.bottleHeight = data.commodityInfo.bottleHeight
                this.dataForm.deepCategoryId = data.commodityInfo.deepCategoryId
                this.dataForm.deepCategoryName = data.commodityInfo.deepCategoryName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/commodity/commodityinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'commodityName': this.dataForm.commodityName,
                'commodityNo': this.dataForm.commodityNo,
                'commodityType': this.dataForm.commodityType,
                'bottleProductId': this.dataForm.bottleProductId,
                'modelId': this.dataForm.modelId,
                'bottleCapId': this.dataForm.bottleCapId,
                'bottleSprayHeadId': this.dataForm.bottleSprayHeadId,
                'bottlePartId': this.dataForm.bottlePartId,
                'clientId': this.dataForm.clientId,
                'centerCoverId': this.dataForm.centerCoverId,
                'imagePath': this.dataForm.imagePath,
                'status': this.dataForm.status,
                'commodityPrice': this.dataForm.commodityPrice,
                'createTime': this.dataForm.createTime,
                'createUser': this.dataForm.createUser,
                'updateUser': this.dataForm.updateUser,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark,
                'productWeight': this.dataForm.productWeight,
                'productVolume': this.dataForm.productVolume,
                'bottleOutDiameter': this.dataForm.bottleOutDiameter,
                'version': this.dataForm.version,
                'bottleHeight': this.dataForm.bottleHeight,
                'deepCategoryId': this.dataForm.deepCategoryId,
                'deepCategoryName': this.dataForm.deepCategoryName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
